namespace Project.Library.NHibernate
{
    /// <summary>
    /// Create an implementation of an implementation of a Unit of Work
    /// </summary>
    public class NHunitOfWorkFactory : INHunitOfWorkFactory
    {
        private readonly INHSessionFactory sessionFactory;
        private readonly IActiveSessionManager activeSessionManager;
        private readonly ITransactionManager transactionManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="NHunitOfWorkFactory"/> class.
        /// </summary>
        /// <param name="sessionFactory">The session factory.</param>
        /// <param name="activeSessionManager">The active session manager.</param>
        /// <param name="transactionManager">The transaction manager.</param>
        public NHunitOfWorkFactory(INHSessionFactory sessionFactory, IActiveSessionManager activeSessionManager, ITransactionManager transactionManager)
        {
            this.sessionFactory = sessionFactory;
            this.transactionManager = transactionManager;
            this.activeSessionManager = activeSessionManager;
        }

        /// <summary>
        /// Creates this instance.
        /// </summary>
        /// <returns></returns>
        public INHUnitOfWork Create()
        {
            var session = sessionFactory.Create();
            activeSessionManager.SetActiveSession(session);
            transactionManager.SetSession(session);
            return new NHUnitOfWorkImpl(activeSessionManager, transactionManager);
        }
    }
}